/*******************************************************************
*
*    DESCRIPTION:main.cpp
*
*    AUTHOR:LiuHui
*
*    HISTORY:
*
*    DATE:8/19/2006
*
*******************************************************************/
#include "config.h"
#include "head.h"
#include "iptables.h"
#include "trayicon.h"
#include "outputmonitor.h"
#include "rulescleaner.h"
#include "mainwindow.h"
//global variables
GladeXML *gui_xml; 
xmlDocPtr conf_xml;
/*
 *Function Name:ByeBye
 *
 *Parameters:called before exit
 *
 *Description:
 *
 *Returns:
 *
 */
void ByeBye(void)
{
	char cmd[128];
	memset(cmd, 0, 128);
	sprintf(cmd, "iptables -F");
	system(cmd);
	sprintf(cmd, "iptables -P INPUT ACCEPT");
	system(cmd);

	printf("ByeBye\n");
}
/*
 *Function Name:main
 *
 *Parameters:
 *
 *Description:
 *
 *Returns:
 *
 */

int main(int argc, char *argv[])
{
	//detect modules
	//detect SMP

	//init the gui
	gtk_init(&argc, &argv);
	gui_xml = glade_xml_new(GUI_PATH, NULL, NULL);
	glade_xml_signal_autoconnect(gui_xml);
	//find out if the software is started in first time
	conf_xml = XmlInit(CONFIG_PATH);
	int i = IsFirstStart(conf_xml);
	if ( i > 0 )
	{
		//show welcome dialog
		GtkWidget *dialog_welcome = glade_xml_get_widget(gui_xml, "dialog_welcome");
		gtk_widget_show(dialog_welcome);
	}
	else
	{

		GtkWidget *dialog_welcome = glade_xml_get_widget(gui_xml, "dialog_welcome");
		gtk_widget_hide(dialog_welcome);

		IptablesInitByXml();
		MainWindowInit();
		//create a thread to monitor the package
		pthread_t tid;
		pthread_t tid1;
		pthread_create(&tid, NULL, &OutputMonitor, NULL);
		pthread_create(&tid1, NULL, &RulesCleaner, NULL);
		//create notify icon
		GtkWidget *menu_notify = glade_xml_get_widget(gui_xml, "menu_notify");
		CreateTray(menu_notify);
	}
	i = atexit(ByeBye);
	if ( i != 0 )
	{
		printf("set exit function error\n");
	}
	gtk_main();
	return 0;
}
